package com.jy.adscm.operation.hscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hscrb.bean.LjzzPartsmade;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzPartsassemblyinfoMapper;
import com.jy.adscm.operation.hscrb.mapper.mysql.LjzzPartsmadeMapper;
import com.jy.adscm.operation.hscrb.service.LjzzPartsmadeService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-23
 */
@Service
public class LjzzPartsmadeServiceImpl extends ServiceImpl<LjzzPartsmadeMapper, LjzzPartsmade> implements LjzzPartsmadeService {
   @Autowired
   LjzzPartsmadeMapper ljzzPartsmadeMapper;
   @Autowired
   JqbzPartsassemblyinfoMapper jqbzPartsassemblyinfoMapper;
   @Autowired
    JyUserMapper jyUserMapper;

    @Override
    public Boolean updateLjzzPartsmade(LjzzPartsmade ljzzPartsmade, Token token) throws AdscmSystemException {
        //待办修改
        if (ljzzPartsmade.getTaskstate()==0 && ljzzPartsmade.getTaskfinishstate()==0 &&
                "0".equals(ljzzPartsmade.getIstaskstart()) &&  "0".equals(ljzzPartsmade.getIstaskbegin()) &&
                "0".equals(ljzzPartsmade.getIstaskend())  && "0".equals(ljzzPartsmade.getIsreviewtask())
        ){


            return  super.updateById(ljzzPartsmade);
        }
        //接收任务
        if (ljzzPartsmade.getTaskstate()==0 && ljzzPartsmade.getTaskfinishstate()==0 &&
            "1".equals(ljzzPartsmade.getIstaskstart()) &&  "0".equals(ljzzPartsmade.getIstaskbegin()) &&
             "0".equals(ljzzPartsmade.getIstaskend())  && "0".equals(ljzzPartsmade.getIsreviewtask())
           ){
            ljzzPartsmade.setIstaskstart("1");
            ljzzPartsmade.setTaskstartaddtime(new Date());
            ljzzPartsmade.setTaskstate(1);

            return  super.updateById(ljzzPartsmade);
        }
        //开始任务
        if (ljzzPartsmade.getTaskstate()==1 && ljzzPartsmade.getTaskfinishstate()==0 &&
                "1".equals(ljzzPartsmade.getIstaskstart()) &&  "1".equals(ljzzPartsmade.getIstaskbegin()) &&
                "0".equals(ljzzPartsmade.getIstaskend())  && "0".equals(ljzzPartsmade.getIsreviewtask())
        ){
            ljzzPartsmade.setIstaskbegin("1");
            ljzzPartsmade.setTaskbeginaddtime(new Date());
            return super.updateById(ljzzPartsmade);
        }
        //结束任务
        if (ljzzPartsmade.getTaskstate()==1 && ljzzPartsmade.getTaskfinishstate()==0 &&
                "1".equals(ljzzPartsmade.getIstaskstart()) &&  "1".equals(ljzzPartsmade.getIstaskbegin()) &&
                "1".equals(ljzzPartsmade.getIstaskend())  && "0".equals(ljzzPartsmade.getIsreviewtask())
        ){
            ljzzPartsmade.setIstaskend("1");
            ljzzPartsmade.setTaskfinishstate(1);
            ljzzPartsmade.setTaskendaddtime(new Date());
            return super.updateById(ljzzPartsmade);
        }
        //复核任务
        if (ljzzPartsmade.getTaskstate()==1 && ljzzPartsmade.getTaskfinishstate()==1 &&
                "1".equals(ljzzPartsmade.getIstaskstart()) &&  "1".equals(ljzzPartsmade.getIstaskbegin()) &&
                "1".equals(ljzzPartsmade.getIstaskend())  && "1".equals(ljzzPartsmade.getIsreviewtask())
        ){
            ljzzPartsmade.setIsreviewtask("1");
            ljzzPartsmade.setReviewaddtime(new Date());
            ljzzPartsmade.setReviewperson(token.getUser().getId().toString());
            return super.updateById(ljzzPartsmade);
        }

        return null;
    }



    @Override
    public List<LjzzPartsmade> selectLjzzPartsmade(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
            Wrapper<LjzzPartsmade> wrapper =new EntityWrapper<>();
            wrapper.eq("TASKSTATE",taskState);
            wrapper.eq("TASKFINISHSTATE",taskFinishState);
            wrapper.eq("isReviewTask",isReviewTask);
        List<LjzzPartsmade> ljzzPartsmades= ljzzPartsmadeMapper.selectList(wrapper);



        for (LjzzPartsmade ljzzPartsmade : ljzzPartsmades) {
            if ( ljzzPartsmade.getTaskreleaseperson()!=null && !"".equals( ljzzPartsmade.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(ljzzPartsmade.getTaskreleaseperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    ljzzPartsmade.setTaskreleasepersonName(jyUser.getName());
                }else {
                    ljzzPartsmade.setTaskreleasepersonName(ljzzPartsmade.getTaskreleaseperson());
                }

            }
            if ( ljzzPartsmade.getTaskperformperson()!=null && !"".equals( ljzzPartsmade.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(ljzzPartsmade.getTaskperformperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    ljzzPartsmade.setTaskperformpersonName(jyUser.getName());
                }else {
                    ljzzPartsmade.setTaskperformpersonName(ljzzPartsmade.getTaskperformperson());
                }

            }

            if ( ljzzPartsmade.getReviewperson()!=null && !"".equals( ljzzPartsmade.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(ljzzPartsmade.getReviewperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    ljzzPartsmade.setReviewpersonName(jyUser.getName());
                }else {
                    ljzzPartsmade.setReviewpersonName(ljzzPartsmade.getReviewperson());
                }

            }

        }
        
        return ljzzPartsmades;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("93");
        return jyUsers;
    }
}
